﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EF.Model;
using EF.Model.Base;
using EF.Util;
using EF.Util.DBUtility;

namespace EF.DAL.Base
{
    public class RoleDAO
    {
        /// <summary>
        /// 获取角色Id和角色名
        /// </summary>
        /// <param name="lang">语言</param>
        /// <returns></returns>
        public DataTable GetShortList(string lang)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10)
                                       };

            sqlparams[0].Value = lang;

            SqlDataReader dr = DbHelperSQL.RunProcedure("spBaseRoleShortList", sqlparams);
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();

            return dt;
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="lang">语言</param>
        /// <param name="columns">显示的列</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">一页的记录数</param>
        /// <param name="orderBy">排序</param>
        /// <param name="rowsCount">总记录数</param>
        /// <returns></returns>
        public DataTable GetRoleList(string lang, string columns, int? pageIndex, int? pageSize, string orderBy, out int rowsCount)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@columns", SqlDbType.NVarChar, 1000),
                                           new SqlParameter("@pageIndex", SqlDbType.Int, 4),
                                           new SqlParameter("@pageSize", SqlDbType.Int, 4),
                                           new SqlParameter("@orderBy", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@rowsCount", SqlDbType.Int, 4)
                                       };

            sqlparams[0].Value = lang;
            sqlparams[1].Value = columns;
            sqlparams[2].Value = pageIndex;
            sqlparams[3].Value = pageSize;
            sqlparams[4].Value = orderBy;
            sqlparams[5].Direction = ParameterDirection.Output;

            SqlDataReader dr = DbHelperSQL.RunProcedure("spBaseRoleList", sqlparams);
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();

            rowsCount = (int)sqlparams[5].Value;

            return dt;
        }

        /// <summary>
        /// 获取角色Model
        /// </summary>
        /// <param name="lang">语言</param>
        /// <param name="roleId">角色Id</param>
        /// <returns></returns>
        public RoleModel GetModel(string lang, int roleId)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@roleId", SqlDbType.Int)
                                       };

            sqlparams[0].Value = lang;
            sqlparams[1].Value = roleId;

            SqlDataReader dr = DbHelperSQL.RunProcedure("spBaseRoleModel", sqlparams);

            RoleModel model = null;

            while (dr.Read())
            {
                model = new RoleModel();

                model.RoleId = Convert.ToInt32(dr["RoleId"].ToString());
                model.RoleMark = dr["RoleMark"].ToString();
                model.RoleNameCn = dr["RoleNameCn"].ToString();

                if (dr["RoleNameEn"] != null)
                {
                    model.RoleNameEn = dr["RoleNameEn"].ToString();
                }

                if (dr["RoleNameJa"] != null)
                {
                    model.RoleNameJa = dr["RoleNameJa"].ToString();
                }

                if (dr["Descr"] != null)
                {
                    model.Descr = dr["Descr"].ToString();
                }

                if (dr["SortIndex"] != null && dr["SortIndex"].ToString() != "")
                {
                    model.SortIndex = Convert.ToInt32(dr["SortIndex"].ToString());
                }

                if (dr["CreateTime"] != null && dr["CreateTime"].ToString() != "")
                {
                    model.CreateTime = Convert.ToDateTime(dr["CreateTime"].ToString());
                }

                if (dr["UpdateTime"] != null && dr["UpdateTime"].ToString() != "")
                {
                    model.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }

            return model;
        }

        /// <summary>
        /// 角色新建
        /// </summary>
        /// <param name="model">角色Model</param>
        /// <returns>1,新建成功;2,新建失败,用户名已存在;0,新建失败,数据库异常.</returns>
        public int Add(RoleModel model, string lang, out string msg)
        {
            SqlParameter[] sqlparams = {
                                         new SqlParameter("@RoleMark", SqlDbType.NVarChar, 20),
                                         new SqlParameter("@RoleNameCn", SqlDbType.NVarChar, 20),
                                         new SqlParameter("@RoleNameEn", SqlDbType.NVarChar, 20),
                                         new SqlParameter("@RoleNameJa", SqlDbType.NVarChar, 20),
                                         new SqlParameter("@Descr", SqlDbType.NVarChar, 50),
                                         new SqlParameter("@SortIndex", SqlDbType.Int, 4),
                                         new SqlParameter("@createUser", SqlDbType.Int, 4),
                                         new SqlParameter("@result", SqlDbType.Int, 4),
                                         new SqlParameter("@lang",SqlDbType.NVarChar, 10),
                                         new SqlParameter("@msg", SqlDbType.NVarChar, 300)
                                     };

            sqlparams[0].Value = model.RoleMark;
            sqlparams[1].Value = model.RoleNameCn;
            sqlparams[2].Value = model.RoleNameEn;
            sqlparams[3].Value = model.RoleNameJa;
            sqlparams[4].Value = model.Descr;
            sqlparams[5].Value = model.SortIndex;
            sqlparams[6].Value = model.CreateUser;
            sqlparams[7].Direction = ParameterDirection.Output;
            sqlparams[8].Value = lang;
            sqlparams[9].Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedure("spBaseRoleAdd", sqlparams);

            msg = sqlparams[9].Value.ToString();

            return (int)sqlparams[7].Value;
        }

        /// <summary>
        /// 角色编辑
        /// </summary>
        /// <param name="model">角色Model</param>
        /// <returns>1,编辑成功;0,编辑失败,数据库异常.</returns>
        public int Edit(RoleModel model, string lang, out string msg)
        {
            SqlParameter[] sqlparams = {
                                         new SqlParameter("@RoleId", SqlDbType.NVarChar, 20),
                                         new SqlParameter("@RoleMark", SqlDbType.NVarChar, 20),
                                         new SqlParameter("@RoleNameCn", SqlDbType.NVarChar, 20),
                                         new SqlParameter("@RoleNameEn", SqlDbType.NVarChar, 20),
                                         new SqlParameter("@RoleNameJa", SqlDbType.NVarChar, 20),
                                         new SqlParameter("@Descr", SqlDbType.NVarChar, 50),
                                         new SqlParameter("@SortIndex", SqlDbType.Int, 4),
                                            new SqlParameter("@updateUser", SqlDbType.Int, 4),
                                            new SqlParameter("@result", SqlDbType.Int, 4),
                                            new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                            new SqlParameter("@msg", SqlDbType.NVarChar, 300)
                                       };

            sqlparams[0].Value = model.RoleId;
            sqlparams[1].Value = model.RoleMark;
            sqlparams[2].Value = model.RoleNameCn;
            sqlparams[3].Value = model.RoleNameEn;
            sqlparams[4].Value = model.RoleNameJa;
            sqlparams[5].Value = model.Descr;
            sqlparams[6].Value = model.SortIndex;
            sqlparams[7].Value = model.UpdateUser;
            sqlparams[8].Direction = ParameterDirection.Output;
            sqlparams[9].Value = lang;
            sqlparams[10].Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedure("spBaseRoleEdit", sqlparams);

            msg = sqlparams[10].Value.ToString();

            return (int)sqlparams[8].Value;
        }

        /// <summary>
        /// 角色逻辑删除
        /// </summary>
        /// <param name="roleIds">被逻辑删除角色Id</param>
        /// <param name="updateUser">执行逻辑删除用户Id</param>
        /// <returns>1,逻辑删除成功;0,逻辑删除失败,数据库异常.</returns>
        public int LogicDel(string roleIds, int updateUser, string lang, out string msg)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@roleIds", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@updateUser", SqlDbType.Int, 4),
                                           new SqlParameter("@result", SqlDbType.Int, 4),
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@msg", SqlDbType.NVarChar, 300)
                                       };

            sqlparams[0].Value = roleIds;
            sqlparams[1].Value = updateUser;
            sqlparams[2].Direction = ParameterDirection.Output;
            sqlparams[3].Value = lang;
            sqlparams[4].Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedure("spBaseRoleLogicDel", sqlparams);

            msg = sqlparams[4].Value.ToString();

            return (int)sqlparams[2].Value;
        }

        /// <summary>
        /// 获取功能点树
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="lang">语言</param>
        /// <returns></returns>
        public DataTable GetFunctionTree(int roleId, string lang)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@roleId", SqlDbType.Int),
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10)
                                       };

            sqlparams[0].Value = roleId;
            sqlparams[1].Value = lang;

            SqlDataReader dr = DbHelperSQL.RunProcedure("spBaseRoleFunction", sqlparams);
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();

            return dt;
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        /// <param name="funcRoleId">功能点Id</param>
        /// <param name="roleId">角色Id</param>
        /// <param name="lang">语言</param>
        /// <returns>1,设置成功;0,设置失败,数据库异常.</returns>
        public int SetFunctionRole(string funcRoleId, int roleId, int userId, string lang, out string msg)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@funcRoleId", SqlDbType.NVarChar, 500),
                                           new SqlParameter("@roleId", SqlDbType.Int),
                                           new SqlParameter("@userId", SqlDbType.Int),
                                           new SqlParameter("@result", SqlDbType.Int, 4),
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@msg", SqlDbType.NVarChar, 300)
                                       };

            sqlparams[0].Value = funcRoleId;
            sqlparams[1].Value = roleId;
            sqlparams[2].Value = userId;
            sqlparams[3].Direction = ParameterDirection.Output;
            sqlparams[4].Value = lang;
            sqlparams[5].Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedure("spBaseRoleSetFunc", sqlparams);

            msg = sqlparams[5].Value.ToString();

            return (int)sqlparams[3].Value;
        }

    }
}
